#!/usr/bin/env python

import datetime
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relation, backref, sessionmaker

metadata = MetaData()
Base = declarative_base(metadata=metadata)

trace_application = Table('trace_application', metadata, 
                    Column('trace_id', Integer, ForeignKey('trace.id')),
                    Column('application_id', Integer, ForeignKey('application.id')))

class Trace(Base):
    __tablename__ = 'trace'
    
    id = Column(Integer, primary_key=True)
    date = Column(DateTime)
    net_available = Column(Boolean)
    proc_load = Column(Integer)
    mem_load = Column(Integer)
    nb_process = Column(Integer)
    nb_process_running = Column(Integer)
    
    apps = relation('Application', secondary=trace_application, backref='trace')
    
    def __init__(self, proc_load=None, mem_load=None, nb_process_running=None, nb_process=None, net_available=None, date=None):
        self.date = datetime.datetime.now() if not date else date
        self.proc_load = proc_load
        self.mem_load = mem_load
        self.nb_process = nb_process
        self.nb_process_running = nb_process_running
        self.net_available = net_available
    
    def __str__(self):
        return "Trace @%s" % self.date

class Application(Base):
    __tablename__ = 'application'
    
    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True)
    
    def __init__(self, name):
        self.name = name
        
    def __str__(self):
        return "Application %s" % self.name
